using System;
using System.IO;
using System.Text;
using BlogEngine.Core.DataStore;
using System.Xml;
using System.Xml.Serialization;
using System.Web.Hosting;
using Eloquera.Client;
using BlogEngine.Core.Providers.EloqueraProvider.Db;

namespace BlogEngine.Core.Providers
{
  /// <summary>
  /// A storage provider for BlogEngine that uses XML files.
  /// <remarks>
  /// To build another provider, you can just copy and modify
  /// this one. Then add it to the web.config's BlogEngine section.
  /// </remarks>
  /// </summary>
    public partial class EloqueraBlogProvider : BlogProvider
  {
    /// <summary>
    /// Loads settings from generic data store
    /// </summary>
    /// <param name="exType">Extension Type</param>
    /// <param name="exId">Extension ID</param>
    /// <returns>Stream Settings</returns>
        public override object LoadFromDataStore(ExtensionType exType, string exId) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = exId;
            param["type"] = (int)exType;
            EqDataStore eqDs = (EqDataStore)EqDb.ExecuteScalar("select EqDataStore where ExId = @id and ExType = @type", param);

            return eqDs == null ? null : eqDs.XmlContent;
        }

    /// <summary>
    /// Save settings to generic data store
    /// </summary>
    /// <param name="exType">Type of extension</param>
    /// <param name="exId">Extension ID</param>
    /// <param name="settings">Stream Settings</param>
        public override void SaveToDataStore(ExtensionType exType, string exId, object settings) {

            try {
                Parameters param = EqDb.CreateParameters();
                param["id"] = exId;
                param["type"] = (int)exType;
                EqDataStore eqDs = (EqDataStore)EqDb.ExecuteScalar("select EqDataStore where ExId = @id and ExType = @type", param);
                if (eqDs==null)
                    eqDs = new EqDataStore() {
                        ExId = exId,
                        ExType = (int)exType
                    };
                StringWriter writer = new StringWriter();
                XmlSerializer x = new XmlSerializer(settings.GetType());
                x.Serialize(writer, settings);
                writer.Close();
                eqDs.XmlContent = writer.ToString();
                EqDb.Store(eqDs);
            } catch (Exception e) {
                string s = e.Message;
                throw;
            }
        }

    /// <summary>
    /// Removes settings from data store
    /// </summary>
    /// <param name="exType">Extension Type</param>
    /// <param name="exId">Extension Id</param>
        public override void RemoveFromDataStore(DataStore.ExtensionType exType, string exId) {
            Parameters param = EqDb.CreateParameters();
            param["id"] = exId;
            param["type"] = (int)exType;
            EqDataStore eqDs = (EqDataStore)EqDb.ExecuteScalar("select EqDataStore where ExId = @id and ExType = @type", param);
            EqDb.Delete(eqDs);
        }

    /// <summary>
    /// Data Store Location
    /// </summary>
    /// <param name="exType">Type of extension</param>
    /// <returns>Path to storage directory</returns>
    private string StorageLocation(ExtensionType exType)
    {
        return string.Empty;
      switch (exType)
      {
        case ExtensionType.Extension:
          return HostingEnvironment.MapPath(Path.Combine(BlogSettings.Instance.StorageLocation, @"datastore\extensions\"));
        case ExtensionType.Widget:
          return HostingEnvironment.MapPath(Path.Combine(BlogSettings.Instance.StorageLocation, @"datastore\widgets\"));
        case ExtensionType.Theme:
          return HostingEnvironment.MapPath(Path.Combine(BlogSettings.Instance.StorageLocation, @"datastore\themes\"));
      }
      return string.Empty;
    }
  }
}

